package com.kd.controller.recruiter;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;
import com.kd.bean.recruiter.Recruiter_basic;
import com.kd.service.recruiter.IRecruiter_basicService;

/**
 * 招聘者
 * 
 * @author ZFC
 *
 */
@Controller
@RequestMapping("/recruiter")
public class Recruiter_basicController {

	@Autowired
	private IRecruiter_basicService recService;

	@RequestMapping("/queryAllBasicMessgae")
	@ResponseBody
	public PageInfo<Recruiter_basic> queryAllBasicMessgae(Integer pageNum, Integer pageSize) {
		// 管理员操作 分页显示所有招聘者信息，
		PageInfo<Recruiter_basic> info = null;

		try {
			info = recService.queryAllBasicMessgae(pageNum, pageSize);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return info;
	}

	@RequestMapping("/queryBasicMessgaeByStatei")
	@ResponseBody
	public PageInfo<Recruiter_basic> queryBasicMessgaeByStatei(Integer pageNum, Integer pageSize) {
		// 管理员操作 分页显示所有刚刚注册的招聘者信息状态为1的
		PageInfo<Recruiter_basic> info = null;

		try {
			info = recService.queryBasicMessgaeByStatei(pageNum, pageSize);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return info;
	}

	@RequestMapping("/updateBasicState")
	@ResponseBody
	public Boolean updateBasicState(Integer recruiter_id, Integer recruiter_statei) {
		// 管理员操作 管理员审核通过后修改状态为启用状态查看举报招聘者信息，如果确实违规可对招聘者进行禁用
		Boolean flag = false;
		try {

			flag = recService.updateBasicState(recruiter_id, recruiter_statei);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}

	@RequestMapping("/queryRecPersonCount")
	@ResponseBody
	public Integer queryRecPersonCount() {
		// 管理员 招聘人数
		Integer recCount = 0;
		try {
			recCount = recService.queryRecPersonCount();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return recCount;
	}

	@RequestMapping("/basicLogin")
	public String basicLogin(Recruiter_basic basic, Model model, HttpSession session) {
		// 招聘者登录

		try {
			Recruiter_basic basicMsg = recService.basicLogin(basic);
			if (basicMsg != null) {
				if(basicMsg.getRecruiter_statei()==1) {
					String msg = "该账号禁止登录！！！";
					model.addAttribute("msg", msg);
					return "/view/login.jsp";
				}
				session.setAttribute("basicMsg", basicMsg);
				return "/view/job/jobMain.jsp";
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String msg = "查询不到该联系人，或手机、密码输入错误！！！";
		model.addAttribute("msg", msg);
		return "/view/login.jsp";
	}

	@RequestMapping("/basicRegister")
	public String basicRegister(Recruiter_basic basic, Model model) {
		// 招聘者注册

		try {
			Boolean basicMsg = recService.basicRegister(basic);
			String msg = basicMsg ? "注册成功！！！" : "注册失败，请仔细检查";
			model.addAttribute("msg", msg);
			if (basicMsg) {
				return "/view/login.jsp";
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "/view/register.jsp";
	}

	@RequestMapping("/queryPwd")
	@ResponseBody
	public Recruiter_basic queryPwd(Recruiter_basic basic) {
		// 招聘者 找回密码通过身份证，手机号，邮箱查看密码

		Recruiter_basic basicPwd = null;
		try {
			basicPwd = recService.queryPwd(basic);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return basicPwd;
	}

	@RequestMapping("/basicEsc")
	public String adminEsc(HttpSession session) {
		session.invalidate();
		return "redirect:/view/login.jsp";
	}
	
	@RequestMapping("/queryByRecruiter_id")
	@ResponseBody
	public Recruiter_basic queryByRecruiter_id(HttpSession session) {
		Recruiter_basic basic = (Recruiter_basic) session.getAttribute("basicMsg");
		Recruiter_basic basicMessage = null; 
		try {
			basicMessage = recService.queryByRecruiter_id(basic.getRecruiter_id());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return basicMessage;
	}

	@RequestMapping("/updateBasic")
	public String updteBasic(Recruiter_basic basic, @RequestParam("myFile") MultipartFile myFile,
			HttpServletRequest request,Model model,HttpSession session) {
		Recruiter_basic basicData = (Recruiter_basic) session.getAttribute("basicMsg");
		Boolean flag = false;
		// 1、设置上传路径
		String storPath = System.getProperty("user.dir") + "/data/tmp/image";
		// 2、判断该位置存在不存在
		File file = new File(storPath);
		if (!file.getParentFile().exists()) {// 如果父路径不存在
			file.mkdirs();// 则创建目录
		}
		// 3、将文件上传到上传路径
		try {
			File file2 = new File(storPath, myFile.getOriginalFilename());
			myFile.transferTo(file2);
			//给头像地址
			basic.setRecruiter_img(myFile.getOriginalFilename());
			basic.setRecruiter_id(basicData.getRecruiter_id());
			flag = recService.updteBasic(basic);
			if(flag) {
				model.addAttribute("msg", "修改成功");
				//修改成功，重新给session赋值
				Recruiter_basic basicMsg = recService.basicLogin(basicData);
				session.setAttribute("basicMsg", basicMsg);
				return "/view/job/jobPersonData.jsp";
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.addAttribute("msg", "修改失败");
		return "/view/job/jobPersonData.jsp";
	}
	
	@RequestMapping("/updateBasicPwd")
	public String updateBasicPwd(String recruiter_ps,String jiu_ps,HttpSession session,Model model) {
		Recruiter_basic basic = (Recruiter_basic) session.getAttribute("basicMsg");
		try {
			if(jiu_ps.equals(basic.getRecruiter_ps())) {
				//旧密码等于新密码
				if(basic.getRecruiter_ps().equals(recruiter_ps)) {
					//旧密码等于修改的密码
					model.addAttribute("msg", "新密码与旧密码不能一样哦！");
					return "/view/job/jobPersonData.jsp";
				}else {
					Boolean flag = recService.updateBasicPwd(recruiter_ps, basic.getRecruiter_id());
					model.addAttribute("msg", flag?"修改成功":"修改失败");
					if(flag) {
						return "/view/login.jsp";
					}
					return "/view/job/jobPersonData.jsp";
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.addAttribute("msg", "与原始密码不一样哦！");
		return "/view/job/jobPersonData.jsp";
	}
	
	@RequestMapping("/queryNewsMessageByRecruiter_id")
	@ResponseBody
	public Recruiter_basic queryNewsMessageByRecruiter_id(Integer recruiter_id) {
		Recruiter_basic recruiter_basic = null;
		try {
			recruiter_basic = recService.queryByRecruiter_id(recruiter_id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return recruiter_basic;
	}
}
